package com.ctcemti.mdm.common.exception.advice;

import com.ctcemti.mdm.common.exception.*;
import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.util.ExceptionStackTrace;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.dao.DataAccessException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理类
* <p>Title: BaseExceptionControllerAdvice</p>  
* <p>Description: </p>  
* @author bxf  
* @date 2019年7月9日
 */
@ControllerAdvice
@Slf4j
public class BaseExceptionControllerAdvice {

    /**
     * 不确定异常处理
     * @param e 不确定异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object unknownExceptionHandler(Exception e) {
        e.printStackTrace();
        log.error("不确定异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
        return new Result(ResultEnum.ZTSJ_1010.getCode(),e.getMessage(), null);
    }

    /**
     * 不确定异常处理
     * @param e 资源不存在异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = UnExitResourcesException.class)
    @ResponseBody
    public Object unknownResourcesExceptionHandler(Exception e) {
        e.printStackTrace();
        log.error("资源不存在异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
        return new Result(ResultEnum.ZTSJ_1010.getCode(),e.getMessage(), null);
    }

    /**
     * sql错误异常处理
     * @param e sql错误抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = DataAccessException.class)
    @ResponseBody
    public Object sqlExceptionHandler(DataAccessException e) {
        log.error("sql异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
        return new Result(ResultEnum.ZTSJ_1009.getCode(),e.getMessage(), null);
    }

    /**
     * mybatis错误异常处理
     * @param e mybatis错误抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = MyBatisSystemException.class)
    @ResponseBody
    public Object ibatisExceptionHandler(MyBatisSystemException e) {
        log.error("mybatis异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
        return new Result(ResultEnum.ZTSJ_1019.getCode(),e.getMessage(), null);
    }

    /**
     * 参数错误异常处理
     * @param e 参数错误抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = InvalidParamException.class)
    @ResponseBody
    public Object invalidParamExceptionHandler(InvalidParamException e) {
    	return new Result(ResultEnum.ZTSJ_1008.getCode(),e.getMessage(), null);
    }

    /**
     * 参数错误异常处理
     * @param e 参数错误抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = ServletRequestBindingException.class)
    @ResponseBody
    public Object invalidParamExceptionHandler(ServletRequestBindingException e) {
    	 log.error("参数错误异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
    	return new Result(ResultEnum.ZTSJ_1011.getCode(),e.getMessage(), null);
    }

    /**
     * 账号密码错误异常处理
     * @param e 账号密码错误抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = IncorrectPasswordException.class)
    @ResponseBody
    public Object incorrectPasswordExceptionHandler(IncorrectPasswordException e) {
        log.error("账号密码错误异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
    	return new Result(ResultEnum.ZTSJ_1003.getCode(),e.getMessage(), null);
    }

    /**
     * 账号被注销异常处理
     * @param e 账号被注销抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = CloseAccountException.class)
    @ResponseBody
    public Object closeAccountExceptionHandler(CloseAccountException e) {
        log.error("账号被注销异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
    	return new Result(ResultEnum.ZTSJ_1012.getCode(),e.getMessage(), null);
    }

    /**
     * 未登录异常处理
     * @param e 未登录抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = NoLoginUserException.class)
    @ResponseBody
    public Object noLoginUserExceptionHandler(NoLoginUserException e) {
        log.error("未登录异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
    	return new Result(ResultEnum.ZTSJ_1013.getCode(),e.getMessage(), null);
    }

    /**
     * 无法删除异常处理
     * @param e 无法删除抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = UnableDeleteException.class)
    @ResponseBody
    public Object unableDeleteExceptionHandler(UnableDeleteException e) {
        log.error("无法删除异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
    	return new Result(ResultEnum.ZTSJ_1014.getCode(),e.getMessage(), null);
    }

    /**
     * 运行异常处理
     * @param e 运行错误抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public Object businessExceptionHandler(BusinessException e) {
    	log.error("运行异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
    	return new Result(ResultEnum.ZTSJ_1015.getCode(),e.getMessage(), null);
    }

    /**
     * 请求格式错误异常处理
     * @param e 请求格式错误抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public Object httpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
    	log.error("请求格式错误异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
    	return new Result(ResultEnum.ZTSJ_1016.getCode(),e.getMessage(), null);
    }

    /**
     * 名称重复异常处理
     * @param e 名称重复错误抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = DuplicateNamesException.class)
    @ResponseBody
    public Object duplicateNamesException(DuplicateNamesException e) {
        log.error("名称重复异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
    	return new Result(ResultEnum.ZTSJ_1005.getCode(),e.getMessage(), null);
    }

    /**
     * 安全验证失败异常处理
     * @param e 安全验证失败抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = SecurityFailedException.class)
    @ResponseBody
    public Object securityFailedException(SecurityFailedException e) {
        log.error("安全验证异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
    	return new Result(ResultEnum.ZTSJ_1017.getCode(),e.getMessage(), null);
    }

    /**
     * 服务启动异常处理
     * @param e 服务启动失败抛出异常
     * @return 统一返回类型实体类
     */
    @ExceptionHandler(value = LicenseFailException.class)
    @ResponseBody
    public Object licenseFailException(LicenseFailException e) {
        log.error("服务启动异常:" + ExceptionStackTrace.getExceptionStackTrace(e));
    	return new Result(ResultEnum.ZTSJ_1018.getCode(),e.getMessage(), null);
    }

}
